import { app, BrowserWindow, ipcMain, Menu } from "electron";
import { closeIcon, settingIcon, resetIcon, rebackIcon, infoIcon } from './icon'
import { createSettingWindow } from "./settingWindow"
import { createInfoWindow } from "./infoWindow"

// 展示右键菜单
function showContextMenu(win: BrowserWindow) {
  ipcMain.on('show-context-menu', (event) => {
    const eventWin = BrowserWindow.fromWebContents(event.sender)
    if(eventWin !== win) return
    const contextMenu = getMenuOptions(win)
    const menu = Menu.buildFromTemplate(contextMenu)
    menu.popup({
      window: win
    });
  })
}

// 返回菜单配置信息
function getMenuOptions(win: BrowserWindow) {
  // 右键菜单配置
  const contextMenu: Electron.MenuItemConstructorOptions[] = [
    {
      label: '重置',
      icon: resetIcon,
      click: () => {
        win.webContents.send('reset-count')
      }
    },
    {
      label: '回退一步',
      icon: rebackIcon,
      click: () => {
        win.webContents.send("reset-one-step")
      }
    },
    {
      label: '统计信息',
      icon: infoIcon,
      click: () => {
        createInfoWindow(win)
      }
    },
    {
      label: '设置',
      icon: settingIcon,
      click: () => {
        createSettingWindow(win)
      }
    },
    {
      type: 'separator'
    },
    {
      label: '退出',
      icon: closeIcon,
      click: () => {
        app.quit()
      }
    }
  ]
  return contextMenu;
}



export {
  showContextMenu,
  getMenuOptions
}
